package graphic;

import graphic.mycomponents.ProgramTable;

import java.awt.Color;
import java.awt.Font;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;

import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import core.Client;
import core.ProgramFile;

public class ProgramTab extends JScrollPane {
	
	protected static ListSelectionListener LISTSELECTIONLISTENER;
	
	String programName;
	
	ProgramTable programTable;
	DefaultTableModel programTableModel;
			
	public ProgramTab(String programName)
	{			
		this.programName = programName;
		
		this.createTable();
		this.populateTable();
		
		super.getViewport().setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE);
		super.setViewportView(this.programTable);
	}
	
	protected void createTable()
	{
		this.programTableModel = new DefaultTableModel()
		{ 
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		
		this.programTableModel.setColumnIdentifiers(ProgramTab.getTableHeader());
		
		this.programTable = new ProgramTable();
		this.programTable.setFont(new Font("Comic Sans MS", Font.BOLD, 15));
		this.programTable.setForeground(Color.black);
		this.programTable.setGridColor(Color.black);
				
		this.programTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.programTable.getSelectionModel().removeListSelectionListener(this.getListSelectionListener());
		this.programTable.getSelectionModel().addListSelectionListener(this.getListSelectionListener());
	}
	
	public String getSelectedFileName()
	{
		try {
			return (String)this.programTable.getValueAt(this.programTable.getSelectedRow(), 1);
		} catch (java.lang.ArrayIndexOutOfBoundsException e) {
			return null;
		}
	}
	
	public boolean isSelectedFileLocked()
	{
		return (Boolean)this.programTable.getValueAt(this.programTable.getSelectedRow(), 6);
	}
	
	protected static Vector<String> getTableHeader()
	{
		Vector<String> header = new Vector<String>();
		header.add("Id");
		header.add("File Name");
		header.add("Last edit by");
		header.add("Last edit When");
		header.add("Rel");
		header.add("Open By");
		header.add(" ");
		
		return header;
	}
	
	protected void populateTable()
	{
		LinkedList<ProgramFile> programFiles = Client.getInstance().getProgramFiles().get(this.programName);
		for (Iterator iterator = programFiles.iterator(); iterator.hasNext();)
		{
			ProgramFile programFile = (ProgramFile) iterator.next();
			this.programTable.getModel().insertRow(this.programTable.getRowCount(), new Object[]{programFile.getFileId(), programFile.getFileName(), programFile.getLast_edit_by(), programFile.getLast_edit_when(), programFile.getRelease_number(), programFile.getLocked_by(), !programFile.getLocked_by().equals(ProgramFile.NONE)});			
		}
	}
	
	public void refreshTable()
	{
		this.createTable();
		this.populateTable();
		
		super.getViewport().setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE);
		super.setViewportView(this.programTable);
		
		super.revalidate();
		super.repaint();
	}

	protected ListSelectionListener getListSelectionListener()
	{
		if(LISTSELECTIONLISTENER == null)
		{
			LISTSELECTIONLISTENER = new ListSelectionListener() {
				
				@Override
				public void valueChanged(ListSelectionEvent e) {

					ListSelectionModel lsm = (ListSelectionModel)e.getSource();
					
			        if(!lsm.isSelectionEmpty())
			        {
			            int minIndex = lsm.getMinSelectionIndex();
			            int maxIndex = lsm.getMaxSelectionIndex();
			            for (int i = minIndex; i <= maxIndex; i++)
			                if (lsm.isSelectedIndex(i))
			                {
			                	int fileId = (Integer)ProgramTab.this.programTable.getValueAt(i, 0);
			                	Client.getInstance().requestReleaseList(fileId);
			                	ReleasesPanel.getInstance().setFileId(fileId);
			                }
			        }
				}
			};
		}
		
		return LISTSELECTIONLISTENER;
	}
}
